package com.bubbles.workspace.sync.controller;


import com.bubbles.engine.common.core.result.Result;
import com.bubbles.workspace.sync.service.source.feature.SourceSyncService;
import com.bubbles.workspace.sync.service.source.feature.SourceTestService;
import com.bubbles.workspace.sync.service.source.metadata.SourceCatalogService;
import com.bubbles.workspace.sync.service.source.metadata.SourceServerService;
import com.bubbles.workspace.sync.vo.SourceCatalogSaveVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>description: 源数据源 </p>
 *
 * @author CryptoNeedle
 * @date 2025-06-23
 */
@Tag(name = "源数据源", description = "负责处理源数据源系统信息")
@RestController
@RequestMapping("/source")
public class SourceController {
    
    @Resource
    private SourceTestService sourceTestService;
    
    @Resource
    private SourceSyncService sourceSyncService;
    
    @Resource
    private SourceServerService sourceServerService;
    
    @Resource
    private SourceCatalogService sourceCatalogService;
    
    @PostMapping("/tree")
    public Result<?> tree(@RequestParam(value = "displayServer") Boolean displayServer, @RequestParam("displayTable") Boolean displayTable) {
        return Result.success(sourceServerService.tree(displayServer, displayTable));
    }
    
    @PostMapping("/catalog/add")
    public Result<?> addSourceCatalog(@RequestBody SourceCatalogSaveVo sourceCatalogSaveVo) {
        sourceCatalogService.addVo(sourceCatalogSaveVo);
        return Result.success();
    }
    
    @PostMapping("/catalog/save")
    public Result<?> saveSourceCatalog(@RequestBody SourceCatalogSaveVo sourceCatalogSaveVo) {
        sourceCatalogService.saveVo(sourceCatalogSaveVo);
        return Result.success();
    }
    
    @PostMapping("/catalog/test/server")
    public Result<Boolean> testServerConnected(@RequestBody SourceCatalogSaveVo sourceCatalogSaveVo) {
        return Result.success(sourceTestService.testServerConnection(sourceCatalogSaveVo.createSourceServer()));
    }
    
    @PostMapping("/catalog/test/jdbc")
    public Result<Boolean> testJdbcConnected(@RequestBody SourceCatalogSaveVo sourceCatalogSaveVo) {
        return Result.success(sourceTestService.testJdbcConnection(sourceCatalogSaveVo.createSourceJdbc(sourceCatalogSaveVo.createSourceServer())));
    }
    
    @PostMapping("/catalog/test/doris")
    public Result<Boolean> testDorisConnected(@RequestBody SourceCatalogSaveVo sourceCatalogSaveVo) {
        return Result.success(sourceTestService.testDorisConnection(sourceCatalogSaveVo.createSourceCatalog(sourceCatalogSaveVo.createSourceJdbc(sourceCatalogSaveVo.createSourceServer()))));
    }
    
    @PostMapping("/catalog/{sourceCatalogName}/enabled")
    public Result<?> enabledSourceCatalog(@PathVariable("sourceCatalogName") String sourceCatalogName, @RequestParam("enabled") boolean enabled) {
        sourceCatalogService.enabledSourceCatalog(sourceCatalogName, enabled);
        return Result.success();
    }
    
    @PostMapping("/catalog/{sourceCatalogName}/sync")
    public Result<?> syncSourceCatalog(@PathVariable("sourceCatalogName") String sourceCatalogName) {
        sourceSyncService.syncSourceCatalog(sourceCatalogName);
        return Result.success();
    }
}